package com.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csu.mypetstore.api.common.CommonResponse;
import com.csu.mypetstore.api.entity.*;
import com.csu.mypetstore.api.persistence.LineItemMapper;
import com.csu.mypetstore.api.persistence.OrderStatusMapper;
import com.csu.mypetstore.api.persistence.OrdersMapper;
import com.csu.mypetstore.api.persistence.SequenceMapper;
import com.csu.mypetstore.api.service.CartService;
import com.csu.mypetstore.api.service.OrderService;
import com.csu.mypetstore.api.vo.CartmessageVO;
import com.csu.mypetstore.api.vo.ItemVO;
import com.csu.mypetstore.api.vo.OrdersVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service("orderService")
public class OrderServiceImpl implements OrderService {


    @Autowired
    private LineItemMapper lineItemMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderStatusMapper orderStatusMapper;

    @Autowired
    private SequenceMapper sequenceMapper;


    @Autowired
    private CartService cartService;


    @Override
    public CommonResponse<List<OrdersVO>> getOrdersByUsername(String username) {////改改改！！！！！！！！！
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",username);//改改改！！！！！！！！！！！！

//        List<Orders> ordersList = ordersMapper.selectList(queryWrapper);

        List<Orders> ordersList = ordersMapper.selectList(queryWrapper);


        if(ordersList.isEmpty()){
            return CommonResponse.createForError("该用户没有订单");
        }

        List<OrdersVO> ordersVOList = new ArrayList<>();
        for(Orders orders : ordersList){
            OrdersVO ordersVO = ordersToOrdersVO(orders);
            ordersVOList.add(ordersVO);
        }

        return CommonResponse.createForSuccess(ordersVOList);
    }

    @Override
    public CommonResponse<OrdersVO> getOrders(int orderId) {
        Orders orders = ordersMapper.selectById(orderId);
        if(orders == null){
            return CommonResponse.createForSuccessMessage("没有该ID的订单");
        }
        OrdersVO ordersVO = ordersToOrdersVO(orders);

        return CommonResponse.createForSuccess(ordersVO);
    }

    @Override
    public CommonResponse<Orders> insertOrders(Orders orders, HttpSession session) {

        //存Order
        Random rand = new Random();
        int orderId = rand.nextInt(1000000)+1;
        while(ordersMapper.selectById(orderId) != null){
            orderId = rand.nextInt(1000000)+1;
        }
        orders.setOrderId(orderId);

        orders.setOrderDate(new Date());
        int result = ordersMapper.insert(orders);

        //存orderStatus
        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderId(orderId);
        orderStatus.setStatus("1");
        orderStatus.setTimestamp(new Date());
        int result1 = orderStatusMapper.insert(orderStatus);

        //存LineItem
        List<LineItem> lineItemList = new ArrayList<>();
        String username = orders.getUsername();
        CommonResponse<List<CartmessageVO>> response = cartService.getCartByUsername(username);
        List<CartmessageVO> cartmessageVOList = response.getData();
        for(int i =0;i<cartmessageVOList.size();i++){
            LineItem lineItem = new LineItem();
            lineItem.setItemId(cartmessageVOList.get(i).getItemId());
            lineItem.setUnitPrice(cartmessageVOList.get(i).getListPrice());
            rand = new Random();
            int linenum = rand.nextInt(1000000)+1;
            lineItem.setLineNumber(linenum);
            lineItem.setQuantity(cartmessageVOList.get(i).getQuantity());
            lineItem.setOrderId(orderId);
            lineItemList.add(lineItem);
            //删除购物车
//            cartService.removeCartByCartId1(cartmessageVOList.get(i).getCartId());
        }

        int result2 = 0;
        for(int i=0;i<lineItemList.size();i++){
            lineItemMapper.insert(lineItemList.get(i));
            if(i==lineItemList.size()-1){
                result2=1;
            }
        }
        if(3==result+result2+result1){
            return CommonResponse.createForSuccess(orders);
        }

        return CommonResponse.createForSuccessMessage("插入失败");
    }

//    @Override
//    public CommonResponse<OrdersVO> insertOrderStatus(OrdersVO ordersVO, HttpSession session) {
//
//        OrderStatus orderStatus = VOToOrderStatus(ordersVO);
//
//        int result = orderStatusMapper.insert(orderStatus);
//        if(1 == result){
//            return CommonResponse.createForSuccess(ordersVO);
//
//        }
//        return CommonResponse.createForSuccessMessage("插入失败");
//    }

    @Override
    public CommonResponse<Orders> updateOrders(Orders orders) {

        int result = ordersMapper.updateById(orders);
        if(1 == result){
            return CommonResponse.createForSuccess(orders);
        }
        return CommonResponse.createForSuccessMessage("修改失败");
    }

    @Override
    public CommonResponse<OrderStatus> updateOrderStatus(OrderStatus orderStatus) {
        int result = orderStatusMapper.updateById(orderStatus);
        if(1 == result){
            return CommonResponse.createForSuccess(orderStatus);
        }
        return CommonResponse.createForSuccessMessage("修改失败");
    }

    @Override
    public CommonResponse<List<LineItem>> getLineItemsByOrderId(int orderId) {
        QueryWrapper<LineItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderid",orderId);
        List<LineItem> lineItemList = lineItemMapper.selectList(queryWrapper);

        if(lineItemList == null){
            return CommonResponse.createForSuccessMessage("没有该ID的lineItem");
        }

        return CommonResponse.createForSuccess(lineItemList);
    }

//    @Override
//    public CommonResponse<lineItem> insertLineItem(lineItem lineItem) {
//        int result = lineItemMapper.insert(lineItem);
//        if(1 == result){
//            return CommonResponse.createForSuccess(lineItem);
//        }
//        return CommonResponse.createForSuccessMessage("插入失败");
//    }


    private OrdersVO ordersToOrdersVO(Orders orders){
        OrdersVO ordersVO = new OrdersVO();

        ordersVO.setOrderId(orders.getOrderId());
        ordersVO.setUsername(orders.getUsername());
        ordersVO.setOrderDate(orders.getOrderDate());
        ordersVO.setShipAddress1(orders.getShipAddress1());
        ordersVO.setShipAddress2(orders.getShipAddress2());
        ordersVO.setShipCity(orders.getShipCity());
        ordersVO.setShipZip(orders.getShipZip());
        ordersVO.setShipState(orders.getShipState());
        ordersVO.setShipCountry(orders.getShipCountry());
        ordersVO.setShipToFirstName(orders.getShipToFirstName());
        ordersVO.setShipToLastName(orders.getShipToLastName());
        ordersVO.setBillAddress1(orders.getBillAddress1());
        ordersVO.setBillAddress2(orders.getBillAddress2());
        ordersVO.setBillCity(orders.getBillCity());
        ordersVO.setBillCountry(orders.getBillCountry());
        ordersVO.setBillState(orders.getBillState());
        ordersVO.setBillZip(orders.getBillZip());
        ordersVO.setBillToLastName(orders.getBillToLastName());
        ordersVO.setBillToFirstName(orders.getBillToFirstName());
        ordersVO.setCourier(orders.getCourier());
        ordersVO.setTotalPrice(orders.getTotalPrice());
        ordersVO.setCreditCard(orders.getCreditCard());
        ordersVO.setExpiryDate(orders.getExpiryDate());
        ordersVO.setCardType(orders.getCardType());
        ordersVO.setLocale(orders.getLocale());


        QueryWrapper<OrderStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderid",orders.getOrderId());
        List<OrderStatus> orderStatusList = orderStatusMapper.selectList(queryWrapper);
        if(orderStatusList.size()>0){
            ordersVO.setStatus(orderStatusList.get(0).getStatus());
            ordersVO.setTimestamp(orderStatusList.get(0).getTimestamp());
        }


        QueryWrapper<LineItem> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("orderid",orders.getOrderId());
        List<LineItem> lineItemList = lineItemMapper.selectList(queryWrapper1);
        ordersVO.setLineItemList(lineItemList);

        return ordersVO;

    }

    private Orders VOToOrders(OrdersVO ordersVO){
        Orders orders  = new Orders();

        orders.setOrderId(ordersVO.getOrderId());
        orders.setUsername(ordersVO.getUsername());
        orders.setOrderDate(ordersVO.getOrderDate());
        orders.setShipAddress1(ordersVO.getShipAddress1());
        orders.setShipAddress2(ordersVO.getShipAddress2());
        orders.setShipCity(ordersVO.getShipCity());
        orders.setShipZip(ordersVO.getShipZip());
        orders.setShipState(ordersVO.getShipState());
        orders.setShipCountry(ordersVO.getShipCountry());
        orders.setShipToFirstName(ordersVO.getShipToFirstName());
        orders.setShipToLastName(ordersVO.getShipToLastName());
        orders.setBillAddress1(ordersVO.getBillAddress1());
        orders.setBillAddress2(ordersVO.getBillAddress2());
        orders.setBillCity(ordersVO.getBillCity());
        orders.setBillCountry(ordersVO.getBillCountry());
        orders.setBillState(ordersVO.getBillState());
        orders.setBillZip(ordersVO.getBillZip());
        orders.setBillToLastName(ordersVO.getBillToLastName());
        orders.setBillToFirstName(ordersVO.getBillToFirstName());
        orders.setCourier(ordersVO.getCourier());
        orders.setTotalPrice(ordersVO.getTotalPrice());
        orders.setCreditCard(ordersVO.getCreditCard());
        orders.setExpiryDate(ordersVO.getExpiryDate());
        orders.setCardType(ordersVO.getCardType());
        orders.setLocale(ordersVO.getLocale());

        return orders;
    }

    private OrderStatus VOToOrderStatus(OrdersVO ordersVO){


        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderId(ordersVO.getOrderId());
        orderStatus.setTimestamp(ordersVO.getTimestamp());
        orderStatus.setStatus(ordersVO.getStatus());

        return orderStatus;
    }

    private List<LineItem> VOTolineItem(OrdersVO ordersVO){
        List<LineItem> lineItemList;

        lineItemList = ordersVO.getLineItemList();

        return lineItemList;
    }

}
